<?php
namespace Cli\Controller;
use Cli\Controller\CommonController;
//       /usr/local/php/bin/php /root/yyy/index.php Cli/Retained/index
class RetainedController extends CommonController {
	protected $analysis_users_model,
			$analysis_equipment_model,
			$retained_users_model,
			$retained_users_detailed_model,
			$retained_device_model,
			$retained_device_detailed_model;
	function _initialize() {
		parent::_initialize();
		$this->analysis_users_model= D("AnalysisUsers");
		$this->analysis_equipment_model= D("AnalysisEquipment");
		$this->retained_users_model = D("RetainedUsers");
		$this->retained_users_detailed_model = D("RetainedUsersDetailed");
		$this->retained_device_model = D("RetainedDevice");
		$this->retained_device_detailed_model = D("RetainedDeviceDetailed");
	}

    public function update(){
    	set_time_limit(0);
		$users = $this->users();
		$devices = $this->devices();
        \Think\Log::record('update users: '.$users.' devices: '.$devices);
	}
	public function users(){
		for($j=30;$j>0;$j--){
			$retained = $users = $user = $nopenids = $cusers = $uopenids = $upday = $upday_detailed = NULL;
			$total = $cuser = 0;
			$time = date('Y-m-d',strtotime("-$j day"));
			$uopenids = $this->retained_users_model->where(array('datetime'=>$time))->find();
			if($uopenids){
				/*
				//循环J天数内每一天
                $k = 2;
                for($i=$j-1;$i>0;$i--){
                    $user = $cusers = NULL;
					$cusers = $this->analysis_users_model
					->where(array("datetime"=>date('Y-m-d',strtotime("-$i day")),'openid'=>array('in',$uopenids['openids'])))
					->select();
					if($cusers){
						foreach($cusers as $cuser){
							$user[] = $cuser['openid'];
						}
					}
					$upday['day'.$k] = count($cusers);
					$upday_detailed['day'.$k] = implode(",",$user);
					$this->retained_users_model->where(array('datetime'=>$time))->save($upday);
					$this->retained_users_detailed_model->where(array('datetime'=>$time))->save($upday_detailed);
	                echo date('Y-m-d',strtotime("-$i day"));
	                echo '   --   '.$i.'  =  '.$k.'                             ';
	                $k++;
				}
				*/
				$cusers = $this->analysis_users_model
				->where(array("datetime"=>date('Y-m-d',strtotime("-1 day")),'openid'=>array('in',$uopenids['openids'])))
				->select();
				if($cusers){
					foreach($cusers as $cuser){
						$user[] = $cuser['openid'];
					}
				}
				$upday['day'.$j] = count($cusers);
				$upday_detailed['day'.$j] = implode(",",$user);
				$this->retained_users_model->where(array('datetime'=>$time))->save($upday);
				$this->retained_users_detailed_model->where(array('datetime'=>$time))->save($upday_detailed);
			}else{
				$nopenids = $this->analysis_users_model->where(array("date(registered)"=>$time))->select();
				$total = $this->analysis_users_model->where(array("datetime"=>$time))->count();
				if($nopenids){
					foreach($nopenids as $nopenid){
						$users[] = $nopenid['openid'];
					}
				}
				$retained['datetime'] = $time;
				$retained['openids'] = implode(",",$users);
				$retained['quantity'] = count($users);
				$retained['total'] = $total;
				$this->retained_users_model->add($retained);
				$upday_detailed['datetime'] = $time;
				$this->retained_users_detailed_model->add($upday_detailed);
			}
		}
			return 'success-数据更新成功';
   	}
	public function devices(){
		for($j=30;$j>0;$j--){
			$retained = $devices = $device = $ndevices = $cdevices = $udevices = $upday = $upday_detailed = NULL;
			$total = 0;
			$time = date('Y-m-d',strtotime("-$j day"));
			$udevices = $this->retained_device_model->where(array('datetime'=>$time))->find();
			if($udevices){
				$cdevices = $this->analysis_equipment_model
				->where(array("datetime"=>date('Y-m-d',strtotime("-1 day")),'device_id'=>array('in',$udevices['devices'])))
				->select();
				if($cdevices){
					foreach($cdevices as $cdevice){
						$device[] = $cdevice['device_id'];
					}
				}
				$upday['day'.$j] = count($cdevices);
				$upday_detailed['day'.$j] = implode(",",$device);
				$this->retained_device_model->where(array('datetime'=>$time))->save($upday);
				$this->retained_device_detailed_model->where(array('datetime'=>$time))->save($upday_detailed);
			}else{
				$ndevices = $this->analysis_equipment_model->where(array("datetime"=>$time,'click_uv'=>array('egt',1)))->select();
				$total = $this->analysis_equipment_model->where(array("datetime"=>$time))->count();
				if($ndevices){
					foreach($ndevices as $ndevice){
						$devices[] = $ndevice['device_id'];
					}
				}
				$retained['datetime'] = $time;
				$retained['devices'] = implode(",",$devices);
				$retained['quantity'] = count($devices);
				$retained['total'] = $total;
				$this->retained_device_model->add($retained);
				$upday_detailed['datetime'] = $time;
				$this->retained_device_detailed_model->add($upday_detailed);
			}
		}
			return 'success-数据更新成功';
   	}

}
